﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ID3_Algorithm.Tree
{
    public class TreeNode
    {
        private Object data;
        private Branch children = null;

        public TreeNode()
        {

        }

        public TreeNode(int nrOfChildren)
        {
            this.children = new Branch(nrOfChildren);
        }

        public TreeNode(Object data):this(data, null)
        {

        }
        public TreeNode(Object data, Branch children) 
        {
            Value = data;
            this.children = children;
        }

        public Object Value 
        {
            get 
            {
                if (this.data.GetType() == typeof(String))
	            {
		            return (string)this.data;
	            }
                else
                {
                    return (Attribute)this.data;
                }
            }
            set { this.data = value; } 
        }

        public TreeNode this[int index]
        {
            get
            {
                if (this.children == null || children.Items.Count+1 < index)
                {
                    return null;
                }
                else
                {
                    return this.children.Items[index];
                }
            }
            set
            {
                if (this.children == null)
                {
                    this.children = new Branch(index);
                    this.children.Items[index] = value;
                }
                else
                {
                    this.children.Items[index] = value;
                }
            }
        }

        public int Children 
        {
            set
            {
                this.children = new Branch(value);
            }
        }

        public List<TreeNode> GetAllChildrenOfANode 
        {
            get {
                if (this.children!= null)
                {
                    return this.children.Items;
                }
                else
                {
                    return null;
                }
            } 
        }

        public void RemoveSpecifiedChild(TreeNode nodeToRemove) 
        {
            this.children.RemoveChildItem(nodeToRemove);
        }
    }
}
